/*
 * Copyright (c) 2022-2023 cetcclout Co., Ltd All rights reserved.
 * Sensor Adapter is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.cetccloud.cimp.adapter.handler;

import cn.hutool.json.JSONObject;
import com.cetccloud.cimp.adapter.handler.abstracts.AbstractInnerMQ;
import com.cetccloud.cimp.adapter.server.mqtt.publish.MqttDtuIdClientService;
import com.cetccloud.cimp.adapter.server.mqtt.publish.MqttPublishClient;

import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.nio.Buffer;

/**
 * @author
 * @apiNote 用于链接和初始化MQTT有关的订阅和发布服务
 * @date 2022/11/23 10:08
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DefaultInnerMQHandler extends AbstractInnerMQ {
    private final static InternalLogger logger = InternalLoggerFactory.getInstance(DefaultInnerMQHandler.class);

    //@Autowired
    //private KafkaTemplate<String,Object> kafkaTemplate;

    @Autowired
    private MqttDtuIdClientService mqttDtuIdClientService;

    @Autowired
    private MqttPublishClient mqttPublishClient;

    @Override
    public void init() {

    }
    
    /**
     * 原始采集数据发布到/do的topic中     
     */
    @Override
    public void fromDtuOrigin(Buffer buffer, Object context) {
        //记录原始数据
//        String resultJsonStr = new String((byte[]) buffer.array());
//        MonitorOriginalDataEntity monitorOriginal =
//                new MonitorOriginalDataEntity(System.currentTimeMillis(),(String)context,(String)context,resultJsonStr,0);
//
//        mqttPublishClient.publishOriginalData(monitorOriginal);

    }

    /**
     * 从dtu中处理并校验通过的数据发布到消息总线指定topic客户端中
     */
    @Override
    public void fromDtu(Buffer buffer, Object context) {
        try {
            String resultJsonStr = new String((byte[]) buffer.array());
            //kafkaTemplate.send(DataContextValue.DATA_TOPIC, resultJsonStr);
            mqttDtuIdClientService.SendMsgSecond(resultJsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.fromDtu(buffer, context);
    }

    /**
     * 处理来自设备查询返回信息
     */
    @Override
    public void fromDtuAT(Buffer buffer, Object context) {
        JSONObject  jsonObject= new JSONObject();
        String resultData = new String((byte[]) buffer.array());
        jsonObject.putByPath("data",resultData);
        jsonObject.putByPath("msg","success");
        jsonObject.putByPath("success",true);
        jsonObject.putByPath("code",0);
        //kafkaTemplate.send(DataContextValue.DATA_TOPIC, jsonObject.toString());
        mqttPublishClient.publishAtBackData(jsonObject, (String)context);
        super.fromDtuAT(buffer, context);
    }

    /**
     * 发送数据到dtu中
     */
    @Override
    public void toDtu(Buffer buffer, Object context) {
        super.toDtu(buffer, context);
    }
}
